package com.pai4j.pgc.dao.community;
import com.pai4j.pgc.entity.community.CCVideoEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 *
 * @author: CYM-pai
 * @date: 2025/10/27 14:39
 **/
@Repository
public interface ICCVideoDAO extends JpaSpecificationExecutor<CCVideoEntity>, JpaRepository<CCVideoEntity, Long> {

    @Transactional
    @Modifying
    @Query("update CCVideoEntity q set q.viewCount = q.viewCount + 1 where q.id = ?1")
    int increxVideoViewCount(Long questionId);

    @Query(value = "select count(1) from pai4j_video v where v.path_id = :ccId and v.status = 1",
            nativeQuery = true)
    Long countCtcVideo(Long ccId);

    List<CCVideoEntity> findAllByPathIdAndStatusOrderByIdDesc(Long pathId, Integer status);
    List<CCVideoEntity> findAllByPathIdOrderByIdAsc(Long pathId);
    List<CCVideoEntity> findAllByPathIdAndIdGreaterThanEqualOrderByIdAsc(Long pathId, Long id);
    List<CCVideoEntity> findAllByPathIdAndStatusNotOrderByIdDesc(Long pathId, Integer status);

    /**
     * 查询状态正常+已解决的置顶问题
     * @param size
     * @return
     */
    @Query(value = "select q.* from pai4j_video q where q.path_id = :pathId and q.recommend = true and q.status in (1, 2) " +
            "order by q.recommend_date desc limit :size", nativeQuery = true)
    List<CCVideoEntity> findHomeRecommendTopQuestion(Long pathId, Integer size);

    /**
     * 查询状态正常+已解决的推荐问题
     * @param
     * @param size
     * @return
     */
    @Query(value = "select q.* from pai4j_video q where q.path_id = :pathId and q.recommend = true " +
            "and q.status = 1 " +
            "order by q.recommend_date desc limit :size", nativeQuery = true)
    List<CCVideoEntity> findHomeRecommendRecommendVideo(Long pathId, Integer size);

    /**
     * 查询状态正常+已解决的悬赏问题
     * @param excludeIds
     * @param size
     * @return
     */
    @Query(value = "select q.* from pai4j_video q where q.path_id = :pathId and q.type = 2 and q.status in (1, 2) " +
            "and q.id not in :excludeIds " +
            "order by q.create_date desc limit :size", nativeQuery = true)
    List<CCVideoEntity> findHomeRecommendRewardQuestion(Long pathId, List<Long> excludeIds, Integer size);

    /**
     * 查询状态正常+已解决的悬赏问题
     * @param excludeIds
     * @param size
     * @return
     */
    @Query(value = "select q.* from pai4j_video q where q.path_id = :pathId and q.status in (1, 2) " +
            "and q.id not in :excludeIds " +
            "order by q.create_date desc limit :size", nativeQuery = true)
    List<CCVideoEntity> findHomeRecommendNewQuestion(Long pathId, List<Long> excludeIds, Integer size);
}
